import { Banner, Menu, IGoodList, IGood, Good, RequestType } from "./declare";
import { observable, action } from "mobx";
import { network } from "@/utils/network";
import Taro from '@tarojs/taro'
import { CartModelCustomer } from "./CartHooksCustomer";

/**
 * 首页数据模型
 */

class HomeDataModel extends CartModelCustomer {
    @observable.ref
    banner: Banner[]

    // @observable.ma
    @observable
    data: any

    @observable
    lucky: any


    @observable
    shopListScroll: IGoodList[]
    @observable
    shopListOne: IGoodList[]
    @observable
    shopListTwo: IGoodList[]

    @observable.ref
    menus: Menu[]

    goodsMap: Map<string, IGood>

    @observable
    loading: boolean

    @observable
    isLogin: boolean

    @observable.ref
    attr: any

    constructor() {
        super()
        this.banner = []
        // 默认给4个 skeleton
        this.data = {};
        this.shopListScroll = [];
        this.shopListOne = [];
        this.shopListTwo = [];
        this.loading = true
        this.menus = []
        this.goodsMap = new Map()
        this.attr = {};
        this.lucky = {};
        this.isLogin = false;
    }

    @action
    setData(data: any[]) {
        this.data = data
        this.shopListScroll = data?.shopListScroll?.strcutData;
        this.shopListOne = data?.shopListOne?.strcutData;
        this.shopListTwo = data?.shopListTwo?.strcutData;
    }

    @action
    setLucky(lucky: any) {
        this.lucky = lucky
    }

    @action
    setShopListScroll(data: any[]) {
        this.shopListScroll = data;
    }
    @action
    setShopListOne(data: any[]) {
        this.shopListOne = data;
    }
    @action
    setShopListTwo(data: any[]) {
        this.shopListTwo = data;
    }


    @action
    setBanner(banner: Banner[]) {
        this.banner = banner
    }

    @action
    setMenu(menus: Menu[]) {
        this.menus = menus
    }

    @action
    setValue(data: IGoodList[]) {
        this.data = data
    }

    async getIsLogin() {
        this.isLogin = !!Taro.getStorageSync('user_id');
    }

    async getData() {
        this.loading = true
        try {
            const res = await network.get('/xxm/home/index')
            if (res.code === 0 && res.data) {
                this.setData(res.data?.solution?.solution?.data);
                this.setLucky(res.data?.lucky);
            }
            this.loading = false
        } catch (e) {
            this.loading = false
        }

        network.get('/api/sys/site/attr?configName=site').then(({ data, code }) => {
            if (code === 0) {
                const list = data.list || [];
                this.attr = list.find(it => it.field === 'technical');
            }
        });

    }
}

export const homeModel = new HomeDataModel()